package com.app.web.jst.order;

import com.alibaba.fastjson.JSONObject;
import com.app.utils.base.http.ResultUtil;
import com.app.utils.base.j2se.*;
import com.app.utils.jst.JstEnum;
import com.app.utils.jst.JstHandler;
import com.app.utils.jst.paramBean.LogisticParam;
import com.app.utils.jst.paramBean.OrderParam;
import com.app.utils.spring.properties.Config;
import com.app.web.goods.goods.GoodsService;
import com.app.web.jst.Order;
import com.app.web.order.OrderService;
import com.app.web.user.info.UserInfo;
import com.app.web.user.info.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Map;

@Api(tags = "聚水潭支付接口")
@RestController
@RequestMapping("/jst/order/")
public class JstOrderController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     *  购买
     * @param uid
     * @param address_id 快递地址id
     * @param sku_id  商品id
     * @param num   购买数量
     * @param units 商品规格
     * @param money 购买总金额
     * @return
     */
    @ApiOperation(value = "生成订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid",value = "用户id(不用传，系统获取)",dataType = "long ",required  = false),
            @ApiImplicitParam(name = "address_id",value = "快递地址id",dataType = "long ",required  = false),
            @ApiImplicitParam(name = "sku_id",value = "商品id",dataType = "String",required  = false),
            @ApiImplicitParam(name = "num",value = "购买数量",dataType = "int",required  = false),
            @ApiImplicitParam(name = "units",value = "商品规格",dataType = "String",required  = false),
            @ApiImplicitParam(name = "money",value = "购买总金额",dataType = "int",required  = false),
            @ApiImplicitParam(name = "type",value = "类型",dataType = "int",required  = false),
            @ApiImplicitParam(name = "payChannel",value = "支付渠道",dataType = "int",required  = false),
            @ApiImplicitParam(name = "goods_name",value = "商品名称",dataType = "String",required  = false),
            @ApiImplicitParam(name = "goods_pic",value = "商品图片",dataType = "int",required  = false)
    })
    @RequestMapping("pay")
    public String pay(@RequestAttribute long uid,  long address_id, String sku_id, int num, String units, String money,
                      @RequestParam(defaultValue = "1")int type , @RequestParam(defaultValue = "1") int payChannel,
                      String goods_name,String goods_pic){
        if (StringHandler.getInt(Config.pp.get("ys.goods.pay.switch"),1)==0) {
            return ResultUtil.error(6001, "该功能研发中...");
        }
        if (Boolean.parseBoolean(Config.pp.get("trading.disable"))) {
            return ResultUtil.error(6001, "交易系统已关闭");
        }
        UserInfo userInfo = userInfoService.info(uid);
        if(Common.isEmpty(userInfo)){
            return ResultUtil.error(6001,"参数异常，请下载最新版本APP");
        }
        if(address_id==0){
            return ResultUtil.error(6001,"请选择收货地址");
        }
        if(Common.isEmpty(units)){
            return ResultUtil.error(6001,"请选择商品规格");
        }
        JSONObject unit = JSONObject.parseObject(units);
        if(StringHandler.yuanToFen(money) != StringHandler.yuanToFen(Convert.objToStr(unit.get("sale_price")))*num){
            return ResultUtil.error(6001,"商品金额异常，请联系客服");
        }
        // 1 在线支付 2 钱包支付 3 支付宝
        long orderId = orderService.orderJst(type,userInfo,goods_name,goods_pic,num,units,StringHandler.yuanToFen(money),address_id,payChannel,sku_id);
        return ResultUtil.success(orderId);
    }

    /**
     * 订单支付成功后通知聚水潭
     * @param orderId 订单id
     */
    @ApiOperation(value = "订单支付成功后通知聚水潭")
    @ApiImplicitParam(name = "orderId",value = "订单id",dataType = "String ",required  = false)
    @RequestMapping("notice/api")
    public String noticeApi(String orderId){

        Map<String,Object> order = orderService.getOrderInfo(StringHandler.getLong(orderId));
        if(StringHandler.isEmpty(order)){
            return ResultUtil.error("601","订单id为空");
        }
        Map<String,Object> map = orderService.addrInfo(StringHandler.getLong(order.get("uid")),StringHandler.getLong(order.get("address_id")));
        OrderParam param = JstHandler.param(order,map);
        Map<String,Object> res = JstHandler.execute(JstEnum.Url.订单上传.getUri(), JsonUtil.toJson(Collections.singletonList(param)));
        Logger.info("通知聚水潭返回信息---"+JsonUtil.toJson(res));
        return ResultUtil.success();
    }
    @ApiOperation(value = "物流查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderSn",value = "订单编号",dataType = "String ",required  = false)
    })
    @RequestMapping("logistics")
    public String logistics(@RequestAttribute long uid,String orderSn){
        LogisticParam param = LogisticParam.builder().so_id(orderSn).build();

        Map<String,Object> res = JstHandler.execute(JstEnum.Url.物流查询.getUri(), JsonUtil.toJson(param));

        return ResultUtil.success();
    }


}
